package org.csu.mypetstore.controller;

import org.csu.mypetstore.domain.*;
import org.csu.mypetstore.service.CartService;
import org.csu.mypetstore.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/catalog")
public class CatalogController {
    @Autowired
    private CatalogService catalogService;

    @Autowired
    private CartService cartService;

    @GetMapping("/view")
    public String view(){
        return "catalog/main";
    }

//    @GetMapping("/viewCart")
//    public String viewCart(){
//        return "catalog/cart";
//    }

    @GetMapping("/addItemToCart")
    public String addItemToCart(String workingItemId,Model model){
        if(workingItemId!=null){
            cartService.insertCartItem(workingItemId,"a",1);
            List<CartItem> cartItems = cartService.getCartItemList("a");
            model.addAttribute("cartItems",cartItems);
            return "catalog/cart";
        }
        return "catalog/main";
    }

    @GetMapping("/viewCategory")
    public String viewCategory(String categoryId, Model model){
        if(categoryId!=null){
            Category category = catalogService.getCategory(categoryId);
            List<Product> productList = catalogService.getProductListByCategory(categoryId);
            model.addAttribute("category",category);
            model.addAttribute("productList",productList);
            return "catalog/category";
        }
        return "catalog/main";
    }

    @GetMapping("/viewProduct")
    public String viewProduct(String productId, Model model){
        if(productId!=null){
            Product product = catalogService.getProduct(productId);
            List<Item> itemList = catalogService.getItemListByProduct(productId);
            model.addAttribute("product",product);
            model.addAttribute("itemList",itemList);

            return  "catalog/product";

        }
        return "catalog/main";
    }

    @GetMapping("/viewItem")
    public String viewItem(String itemId, Model model){
        if(itemId!=null){
            Item item = catalogService.getItem(itemId);
            model.addAttribute("item",item);

            return "catalog/item";

        }
        return "catalog/main";
    }

    @RequestMapping(value="/searchProduct", method= RequestMethod.POST)
    private String doSearchProducts(@RequestParam("keyword") String keyword, Model model) {
        if(keyword!=null) {
            List<Product> productList = catalogService.searchProductList(keyword);
            model.addAttribute("productList",productList);
            return "catalog/searchProduct";
        }
        return "catalog/main";
    }

}
